/**
	This file is part of Fighting Field.

	Fighting Field is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Fighting Field is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Fighting Field.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Button.h"

#include "OgreBorderPanelOverlayElement.h"
#include "OgreTextAreaOverlayElement.h"

#include "GuiListener.h"

namespace FF
{
	Button::Button(const Ogre::String& name, const Style& style, const Ogre::DisplayString& caption, Ogre::Real width)
		: mStyle(style)
	{
		mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate(mStyle.templateName, "BorderPanel", name);
		mBP = (Ogre::BorderPanelOverlayElement*)mElement;
		mTextArea = (Ogre::TextAreaOverlayElement*)mBP->getChild(GetName() + "/ButtonCaption");
		mTextArea->setTop(-(mTextArea->getCharHeight() / 2));

		if (width > 0)
		{
			mElement->setWidth(width);
			mFitToContents = false;
		}
		else mFitToContents = true;

		SetCaption(caption);
		mState = ButtonState::Up;
	}

	const Ogre::DisplayString& Button::GetCaption()
	{
		return mTextArea->getCaption();
	}

	void Button::SetCaption(const Ogre::DisplayString& caption)
	{
		mTextArea->setCaption(caption);
		if (mFitToContents) mElement->setWidth(GetCaptionWidth(caption, mTextArea) + mElement->getHeight() - 12);
	}

	const ButtonState::Val& Button::GetState()
	{
		return mState;
	}

	void Button::_OnCursorPressed(const Ogre::Vector2& cursorPos)
	{
		if (IsCursorOver(mElement, cursorPos, 4)) SetState(ButtonState::Down);
	}

	void Button::_OnCursorReleased(const Ogre::Vector2& cursorPos)
	{
		if (mState == ButtonState::Down)
		{
			SetState(ButtonState::Over);
			if (mListener) mListener->OnButtonHit(this);
		}
	}

	void Button::_OnCursorMoved(const Ogre::Vector2& cursorPos)
	{
		if (IsCursorOver(mElement, cursorPos, 4))
		{
			if (mState == ButtonState::Up) SetState(ButtonState::Over);
		}
		else
		{
			if (mState != ButtonState::Up) SetState(ButtonState::Up);
		}
	}

	void Button::_OnFocusLost()
	{
		SetState(ButtonState::Up);   // reset button if cursor was lost
	}

	void Button::SetState(const ButtonState::Val& bs)
	{
		if (bs == ButtonState::Over)
		{
			mBP->setBorderMaterialName(mStyle.overBorderMaterialName);
			mBP->setMaterialName(mStyle.overMaterialName);
		}
		else if (bs == ButtonState::Up)
		{
			mBP->setBorderMaterialName(mStyle.upBorderMaterialName);
			mBP->setMaterialName(mStyle.upMaterialName);
		}
		else
		{
			mBP->setBorderMaterialName(mStyle.downBorderMaterialName);
			mBP->setMaterialName(mStyle.downMaterialName);
		}

		mState = bs;
	}
}
